Qt.include("oauth.js")
Qt.include("twitter-oauth.js")

var db_name = "sk.mka.twitter";
var db_description = "The TWITTER QML SQL!";
var db_version = "1.0";
var db_expected_size = 10000000;

var STATES = {
    LOGIN: "Login",
    AUTHDONE: "AuthDone"
    };


function checkToken() {
    createToken();
    var db = openDatabase();
    var dataStr = "SELECT * FROM Token";
    db.transaction(function(tx) {
                       var rs = tx.executeSql(dataStr);
                       if(debug)  console.log("rs.rows.length: " + rs.rows.length);
                       if(rs.rows.length > 0)  {
                           screen_name = rs.rows.item(0).screen_name;
                           oauth_token = rs.rows.item(0).token;
                           oauth_token_secret = rs.rows.item(0).oauth_token_secret;
                           state = STATES.AUTHDONE
                       } else {
                           state = STATES.LOGIN;
                       }
                       if(debug){
                       console.log("state: " + state);
                       console.log("screen_name: " + screen_name);
                       console.log("token: " + oauth_token);
                       console.log("oauth_token_secret: " + oauth_token_secret);
                          }
                   });
}

function createToken(){
    var db = openDatabase()
    db.transaction(
                function(tx) {
                    tx.executeSql('CREATE TABLE IF NOT EXISTS Token(screen_name TEXT, token TEXT, oauth_token_secret TEXT)');
                }
                );
}


function saveToken(screen_name, token, token_secret) {
    createToken();
    var db = openDatabase();
    db.transaction(
                function(tx) {
                    var rs = tx.executeSql('SELECT * FROM Token');
                    if (rs.rows.length > 0) {
                        rs = tx.executeSql('UPDATE Token SET screen_name=?, token= ?, oauth_token_secret=?', [screen_name, token, token_secret]);
                        if(main.debug) console.log("update db token : " + token);
                        if(main.debug) console.log("update db token_secret : " + token_secret);

                    }
                    else {
                        rs = tx.executeSql("INSERT INTO Token VALUES (?, ?, ?)", [screen_name, token, token_secret]);
                        if(main.debug) console.log("insert db token: " + token);
                    }
                    state = STATES.AUTHDONE
                }
                );
}



function openDatabase() {
    var db = openDatabaseSync(db_name, db_version, db_description, db_expected_size);
    return db;
}



function dropToken(){
    var db = openDatabase()
    db.transaction(
                function(tx) {
                    tx.executeSql('DROP TABLE IF EXISTS Token');
                }
                );
}


function deleteToken(){
    var db = openDatabase()
    db.transaction(
                function(tx) {
                    tx.executeSql('DELETE FROM Token');
                }
                );
}


